package com.yangxu.hospital.tools.utils;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.oned.Code128Writer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class BarcodeAndQRCodeGenerator {

    /**
     * 生成 QR 码的 Base64 编码字符串
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @return Base64 编码的字符串
     * @throws Exception 如果生成过程中出现错误
     */
    public static String generateQRCodeBase64(String content, int width, int height) throws Exception {
        // 设置二维码参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

        // 生成 BitMatrix
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        // 将 BitMatrix 转换为 BufferedImage
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);

        // 将 BufferedImage 转换为 Base64 字符串
        return convertImageToBase64(image);
    }

    /**
     * 生成条形码的 Base64 编码字符串
     *
     * @param content 内容
     * @param width   宽度
     * @param height  高度
     * @return Base64 编码的字符串
     * @throws Exception 如果生成过程中出现错误
     */
    public static String generateBarcodeBase64(String content, int width, int height) throws Exception {
        // 设置条形码参数
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        // 生成 BitMatrix
        Code128Writer barcodeWriter = new Code128Writer();
        BitMatrix bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height, hints);

        // 将 BitMatrix 转换为 BufferedImage
        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);

        // 将 BufferedImage 转换为 Base64 字符串
        return convertImageToBase64(image);
    }

    /**
     * 将 BufferedImage 转换为 Base64 编码的字符串
     *
     * @param image 图像
     * @return Base64 编码的字符串
     * @throws Exception 如果转换过程中出现错误
     */
    private static String convertImageToBase64(BufferedImage image) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        byte[] imageBytes = baos.toByteArray();
        return Base64.getEncoder().encodeToString(imageBytes);
    }

    public static void main(String[] args) {
        try {
            // 生成 QR 码
            String qrCodeContent = "800000000001";
            int qrCodeWidth = 200;
            int qrCodeHeight = 200;
            String qrCodeBase64 = generateQRCodeBase64(qrCodeContent, qrCodeWidth, qrCodeHeight);
            System.out.println("Base64 QR Code: " + qrCodeBase64);

            // 生成条形码
            String barcodeContent = "800000000001";
            int barcodeWidth = 300;
            int barcodeHeight = 100;
            String barcodeBase64 = generateBarcodeBase64(barcodeContent, barcodeWidth, barcodeHeight);
            System.out.println("Base64 条形码: " + barcodeBase64);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}